package com.vcar.LDiceClient.amfRequester.sz
{
    import com.vcar.tool.HostInfo;

    import flash.events.TimerEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.utils.Timer;

    import mx.rpc.events.ResultEvent;
    public class JobQuery
    {
        private var timer:Timer;
        public var jobId:uint;
        public var handler:Function;
        private var count:int=0;	
        private var methodName:String;	
        public function JobQuery(jobId:uint,handler:Function,methodName:String='')
        {
            trace('JobQuery Create');
            this.jobId=jobId;
            this.handler=handler;
            this.methodName = methodName;

            timer=new Timer(1500,1);
            timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimer);
            timer.start();
        }
        private function onTimer(e:TimerEvent):void
        {			
            trace('JobQuery onTimer');		
            if(DataManager.data.so.manipulationUserId == 0)
            {
                DataManager.data.so.manipulationUserId = DataManager.data.so.uid;
            }
            //trace("=========调用job是否完成：",jobId,"====uid:",DataManager.data.so.manipulationUserId)
            trace('JobQuery call remoteMethod');	
            DataManager.data.userService.hasFinishedJob(DataManager.data.so.manipulationUserId,jobId,onJobQuery,onFail);
            count++;
        }

        /**
         * 修改注解：每一个job不管以何种方式注销掉，都必须触发complete
         * @param e
         *
         */        
        private function onJobQuery(e:ResultEvent):void
        {
            trace('JobQuery remote callback result is : ' + e.result.object);
            if(e.result.success){
                switch(e.result.object){
                    case -1:	//不存在
                    case 2:		//已完成，已读过
                        handler();
                        DataManager.data.so.manipulationUserId=0;
                        JobManager.deleteJob(this);
                        break;
                    case 0:		//未完成
                        if(count<8){
                            timer.start();
                            return;
                        }else{
                            var tstr:String= escape(methodName);
                            try{
                                var urlloader:URLLoader = new URLLoader();
                                urlloader.load(new URLRequest("http://"+HostInfo.getHost()+"/server/_to.jsp?methodName="+tstr+"&&jobId="+jobId));
                            }catch(e:Error){

                            }

//                            DataManager.stopAll=true;
//                            DataManager.data.faultAsk("服务器处理请求超时");
                            handler();
                            DataManager.data.so.manipulationUserId=0;
                            JobManager.deleteJob(this);
                        }
                        break;
                    case 1:		//已完成
                        handler();
                        DataManager.data.so.manipulationUserId=0;
                        JobManager.deleteJob(this);
                        break;
                }

            }
            else
            {
                handler();
                DataManager.data.so.manipulationUserId=0;
                JobManager.deleteJob(this);
            }
        }

        private function onFail():void
        {
            if(count<8){
                timer.start();
                return;
            }else{
                var tstr:String=escape(methodName);
                try{
                    var urlloader:URLLoader = new URLLoader();
                    urlloader.load(new URLRequest("http://"+HostInfo.getHost()+"/server/_to.jsp?a="+tstr));
                }catch(e:Error){
                }

//                DataManager.stopAll=true;
//                DataManager.data.faultAsk("服务器处理请求失败");
                handler();
                DataManager.data.so.manipulationUserId=0;
                JobManager.deleteJob(this);
            }
        }			
    }
}


